from datetime import datetime

from emi.report import IReporter, ReportEvent, ReportEventType


class _ConsoleReporter(IReporter):

    def report(self, event: ReportEvent):
        """
        上报花费时间。
        """
        if event.type == ReportEventType.NORMAL:
            print(f"普通事件[{event.time}]: name = {event.name},msg={event.msg}")
        elif event.type == ReportEventType.LOG:
            tag = event.name
            exception_info = event.exception
            _suffix = ""
            if tag:
                _suffix = f"tag={tag},"
            if event.sub_type == 100:
                type_name = "调试"
            elif event.sub_type == 200:
                type_name = "信息"
            elif event.sub_type == 300:
                type_name = "告警"
            elif event.sub_type == 400:
                type_name = "错误"
            else:
                raise Exception()
            msg = event.msg
            print(f"{type_name}日志[{event.time}]: {_suffix}msg={msg}")
            if exception_info:
                print(exception_info)
        elif event.type == ReportEventType.TIME:
            msg = event.msg
            name = event.name
            seconds = event.seconds
            print(f"耗时[{event.time}]: name = {name},seconds={seconds:.2f},msg={msg}")
            if event.exception:
                print(event.exception)
        elif event.type == ReportEventType.ASSERT:
            msg = event.msg
            exception_info = event.exception
            print(f"判定[{event.time}]: msg={msg}")
            if exception_info:
                print(exception_info)


class _SaveLogReporter(IReporter):
    """
    保存指定日志的log。
    """

    def report(self, event: ReportEvent):
        pass